package com.markerhub.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.markerhub.entity.Blog;
import com.markerhub.entity.Tag;
import com.markerhub.entity.Type;
import com.markerhub.mapper.BlogMapper;
import com.markerhub.mapper.TagMapper;
import com.markerhub.mapper.TypeMapper;
import com.markerhub.service.TypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 关注公众号：MarkerHub
 * @since 2021-04-10
 */
@Service
public class TypeServiceImpl extends ServiceImpl<TypeMapper, Type> implements TypeService {
    @Autowired
    TypeMapper typeMapper;

    @Autowired
    BlogMapper blogMapper;
    //分页查询标签
    @Override
    public IPage<Type> getTypes(long current, long size, String column, int sort, String querycontent){
        QueryWrapper<Type> queryWrapper = new QueryWrapper<>();
        if(querycontent!=null&&querycontent!="")
            queryWrapper.eq("type_name",querycontent);
        //设置升序或者降序
        if( 0 == sort){
            //升序
            queryWrapper.orderByAsc(column);
        }else{
            //降序
            queryWrapper.orderByDesc(column);
        }
        IPage<Type> typeIPage = typeMapper.selectPage(new Page<Type>(current, size), queryWrapper);
        return typeIPage;
    }

    //添加标签
    @Override
    public void addtype(String typename){
        Type type=new Type();
        type.setTypeName(typename);
        typeMapper.insert(type);
    }

    //删除
    @Override
    public void deletetype(Integer typeid){
        QueryWrapper<Type> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("type_id",typeid);
        typeMapper.delete(queryWrapper);
    }

    //修改
    @Override
    public void edit_type(Integer typeid,String typename)
    {
        Type type= typeMapper.selectById(typeid);
        type.setTypeName(typename);
        QueryWrapper<Blog> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("type_id",typeid);
        Blog blog=new Blog();
        blog.setTypeName(typename);
        typeMapper.updateById(type);
        blogMapper.update(blog,queryWrapper);
    }
}
